<template>
  <div style="padding:30px;">
    <el-form ref="queryFrom" :inline="true" :model="queryFrom" class="demo-form-inline">
      <el-form-item prop="code">
        <el-input v-model="queryFrom.question" placeholder="问题" class="el-input--mini" prefix-icon="el-icon-search" maxlength="10" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" class="el-button--mini" @click="queryIssue">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-delete" class="el-button--mini" @click="onReset('queryFrom')">重置</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-plus" class="el-button--mini" @click="onAddKeyword">添加</el-button>
      </el-form-item>
    </el-form>

    <el-table
      :data="tableData"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      border
      lazy
      :row-style="{height:'10px'}"
      :cell-style="{padding:'0px'}"
    >
      <el-table-column
        prop="id"
        label="ID"
        sortable
        width="100"
      />
      <el-table-column
        prop="question"
        label="问题内容"
        width="180"
      />
      <el-table-column
        prop="answer"
        label="问题回复"
        width="480"
      />
      <el-table-column prop="deleted" align="center" label="状态" width="150px">
        <template slot-scope="scope">
          <el-tag :type="scope.row.deleted | tagTypeFilter">
            {{ scope.row.deleted | statusfilters }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="addTime" align="center" label="创建时间" width="210px" sortable>
        <template slot-scope="scope">
          <p>{{ scope.row.addTime | timefilters }}</p>
        </template>
      </el-table-column>
      <el-table-column prop="updatedTime" align="center" label="修改时间" width="210px" sortable>
        <template slot-scope="scope">
          <p>{{ scope.row.updateTime | timefilters }}</p>
        </template>
      </el-table-column>
      <el-table-column prop="id" label="操作" align="center">
        <template slot-scope="scope">
          <div>
            <el-button type="primary" size="small" icon="el-icon-edit" class="el-button--mini" @click="onEditKeyword(scope.row)">编辑</el-button>
            <el-button type="danger" size="small" icon="el-icon-close" class="el-button--mini" @click="onDeleted(scope.row)">{{ scope.row.deleted ===0? '禁用':'启用' }}</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        :total="page.totalCount"
        :current-page="page.currentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="10"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <el-dialog :title="title" :visible.sync="dialogFormVisible">
      <el-form ref="from" :model="from" :rules="rules">
        <el-form-item label="问题内容" :label-width="formLabelWidth" prop="question">
          <el-input
            v-model="from.question"
            size="small"
            placeholder="请输入问题内容"
            autocomplete="off"
            prefix-icon="el-icon-edit"
            maxlength="30"
            show-word-limit
            class="el-input--medium"
          />
        </el-form-item>
        <el-form-item label="问题回复" :label-width="formLabelWidth" prop="answer">
          <el-input
            v-model="from.answer"
            size="small"
            type="textarea"
            :rows="2"
            placeholder="请输入问题回复"
            autocomplete="off"
            prefix-icon="el-icon-edit"
            maxlength="100"
            show-word-limit
            class="el-input--medium"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="el-button--mini" @click="cancelChange('from');dialogFormVisible = false">取 消</el-button>
        <el-button class="el-button--mini" type="primary" @click="onSubmitIssue('from');">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { queryIssue, addShopIssue, updateShopIssue } from '@/api/goods/index'
export default {
  filters: {

    tagTypeFilter(status) {
      return status === 1 ? 'danger' : 'gray'
    },
    isDefaultfilters(val) {
      if (val == null || val === '') {
        return '--'
      } else {
        if (val === 0) {
          return '否'
        }
        if (val === 1) {
          return '是'
        }
      }
    },
    timefilters(val) {
      if (val == null || val === '') {
        return '暂无时间'
      } else {
        const d = new Date(val) // val 为表格内取到的后台时间
        const month =
          d.getMonth() + 1 < 10 ? '0' + (d.getMonth() + 1) : d.getMonth() + 1
        const day = d.getDate() < 10 ? '0' + d.getDate() : d.getDate()
        const hours = d.getHours() < 10 ? '0' + d.getHours() : d.getHours()
        const min = d.getMinutes() < 10 ? '0' + d.getMinutes() : d.getMinutes()
        const sec = d.getSeconds() < 10 ? '0' + d.getSeconds() : d.getSeconds()
        const times = d.getFullYear() + '-' + month + '-' + day + ' ' + hours + ':' + min + ':' + sec
        return times
      }
    },
    statusfilters(val) {
      if (val == null || val === '') {
        return '--'
      } else {
        if (val === 0) {
          return '正常'
        }
        if (val === 1) {
          return '已禁用'
        }
      }
    }

  },

  data() {
    return {
      options: [{
        value: 0,
        label: '正常'
      }, {
        value: 1,
        label: '禁用'
      }],
      isDefaultOptions: [{
        value: 0,
        label: '否'
      }, {
        value: 1,
        label: '是'
      }],
      queryFrom: {
        question: ''
      },
      from: {
        question: '',
        answer: '',
        id: ''
      },
      page: {
        'rowCount': 10,
        'currentPage': 1,
        'pageSize': 10,
        'pageCount': 2,
        'totalCount': 0,
        'orderBy': null,
        'outofBounds': false,
        'limitStart': 0
      },
      tableData: [],
      dialogFormVisible: false,
      title: '',
      isAdd: false,
      formLabelWidth: '90px',
      rules: {
        question: [{ required: true, message: '请填写问题内容', trigger: 'blur' }],
        answer: [{ required: true, message: '请填写问题回复', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.queryIssue()
  },
  methods: {
    queryIssue() {
      const queryPage = { page: this.page }
      var qq = Object.assign(queryPage, this.queryFrom)
      queryIssue(qq).then(response => {
        this.tableData = response.data.list
        this.page = response.data.page
      })
    },
    onAddKeyword() {
      this.dialogFormVisible = true
      this.title = '新增'
      this.isAdd = true
    },
    onEditKeyword(row) {
      this.dialogFormVisible = true
      this.title = '编辑'
      this.isAdd = false

      this.from.question = row.question
      this.from.answer = row.answer
      this.from.id = row.id
    },
    addShopIssue() {
      addShopIssue(this.from).then(response => {
        this.$notify({ title: response.msg, duration: 1000, type: 'success' })
        this.clearForm()
        this.queryIssue()
      })
    },
    onSubmitIssue(formName) {
      this.$refs[formName].validate((valida) => {
        if (valida) {
          if (!this.isAdd) {
            updateShopIssue(this.from).then(response => {
              this.$notify({ title: response.msg, duration: 1000, type: 'success' })
              this.dialogFormVisible = false
              this.clearForm()
              this.queryIssue()
            })
          } else {
            this.from.deleted = false
            addShopIssue(this.from).then(response => {
              this.$notify({ title: response.msg, duration: 1000, type: 'success' })
              this.dialogFormVisible = false
              this.queryIssue()
              this.clearForm()
            })
          }
        } else {
          return false
        }
        this.$refs[formName].clearValidate()
      })
    },
    onDeleted(row) {
      const title = row.deleted === 0 ? '删除' : '启用'
      this.$confirm('确认要' + title + '该问题: ' + row.question + ' 吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.clearForm()
        this.from.id = row.id
        this.from.deleted = row.deleted === 0 ? 1 : 0

        updateShopIssue(this.from).then(response => {
          this.$notify({ title: response.msg, duration: 1000, type: 'success' })
          this.clearForm()
          this.queryIssue()
        })
      }).catch(() => {
      })
    },
    onReset(formName) {
      this.$refs[formName].resetFields()
      this.queryFrom.question = null
    }, handleSizeChange(val) {
      this.page.pageSize = val
      this.queryIssue()
    },
    handleCurrentChange(val) {
      this.page.currentPage = val
      this.queryIssue()
    },
    cancelChange(formName) {
      this.dialogFormVisible = false
      this.$refs[formName].clearValidate()
      this.clearForm()
    },
    clearForm() {
      this.from.question = null
      this.from.answer = null
      this.from.id = null
    }
  }
}
</script>
